<script setup lang='ts'>
  import { onMounted, onActivated, reactive, ref } from 'vue';
  import { gamesApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading, $router } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  import Pagination from "@/components/Pagination/index.vue";
  import rankingForm from './rankingForm.vue';
  import rewardList from './rewardList.vue';
  const rankingFormRef = ref(),
  rewardListRef = ref();
  const action = reactive({
    total:0,
    dialogTableVisible:false,
    activityID:0,
    list:[] as any[],
  })
  const rankingType = [
    {type:1,text:'魅力榜'},
    {type:2,text:'人气榜'},
    {type:3,text:'公会榜'},
    {type:4,text:'房间榜'},
    {type:5,text:'富豪榜'},
  ]
  const rankingRuleType = [
    {type:1,text:'礼物钻石价值'},
    {type:2,text:'人气票'},
    {type:3,text:'礼物钻石价值+人气票	'},
  ]
  const listQuery = reactive({
    page:1,
    limit:20,
    custom_id:'' as any,
  })
  const getType = (type:number = 0)=>{
    return [
      {type:'primary',text:type},
      {type:'success',text:'显示'},
      {type:'danger',text:'不显示'},
    ][type] || {type:'',text:type}
  }
  const handleSerach = ()=>{
    listQuery.page = 1
    getList()
  }
  const openComponents = (row:any = {},path:any)=>{
    const { custom_id } = row
    $router().push({
      path,
      query: {
        activityID:custom_id
      }
    })
  }
  const handleForm = (row:any = {},formRef:any)=>{
    const { custom_id=listQuery.custom_id } = row
    formRef.openDialog({
      ...row,
      custom_id,
      rankingType,
      rankingRuleType
    })
  }
  const handleDelete = (row:any = {})=>{
    const { id=0, game_name='', rank_type_text='' } = row
    if(!id) return ToastWarning('缺少id')
    ElMessageBox.confirm(`确定删除活动：${game_name}-${rank_type_text}配置吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      const loading = Toastloading();
      gamesApi.customRankDel({rank_id:id}).then(()=>{
        ToastSuccess("删除成功!");
        loading.close();
        getList();
      }).catch(()=>{
        loading.close();
      })
    }).catch(() => {});
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await gamesApi.customRankList(listQuery)
    loading.close()
    const { data=[], total=0 } = res.data
    action.total = total
    action.list = data.map((res:any)=>{
      const { rank_type=0 } = res
      return {
        rank_type_text:rankingType.find(res=>res.type == rank_type)?.text || rank_type,
        ...res
      }
    })
  }
  let primarySet = false
  const primaryGetList = async()=>{
    if(primarySet) return
    primarySet = true
    const activityID = $router().currentRoute.value.query?.activityID || ''
    if(listQuery.custom_id != activityID && activityID) {
      listQuery.custom_id = activityID
      const loading = Toastloading()
      await getList()
      primarySet = false
      loading.close()
    }
  }
  onMounted(async()=>{
    primaryGetList()
  })
  onActivated(()=>{
    primaryGetList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">活动ID：</div>
          <el-input type="text" v-model="listQuery.custom_id" placeholder="请输入活动ID" />
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" v-rule="'stencil_configListAdd'" @click="handleForm({},rankingFormRef)">创建榜单</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="120" prop="id" label="ID" />
      <el-table-column width="120" prop="custom_id" label="活动ID" />
      <el-table-column width="220" prop="rank_type_text" label="榜单类型">
        <template #default="scoped">
          {{ scoped.row.rank_type_text }}
          <el-tag
            class="pointer"
            type="primary"
            v-if="scoped.row.rank_type == 2"
            @click="openComponents(scoped.row,'/stencil/ticket')"
          >配置人气票游戏</el-tag>
        </template>
      </el-table-column>
      <el-table-column width="160" prop="if_day" label="日榜">
        <template #default="scoped">
          <el-tag
            :type="getType(scoped.row.if_day)?.type"
          >
          状态：{{ getType(scoped.row.if_day)?.text }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column width="160" prop="if_all" label="总榜">
        <template #default="scoped">
          <el-tag
            :type="getType(scoped.row.if_all)?.type"
          >
          状态：{{ getType(scoped.row.if_all)?.text }}
          </el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column width="180" prop="end_time" label="榜单门槛" /> -->
      <el-table-column min-width="180" prop="score_rule" label="排榜规则">
        <template #default="scoped">
          {{ rankingRuleType.find(res=>res.type == scoped.row.score_rule)?.text || scoped.row.score_rule }}
        </template>
      </el-table-column>
      <el-table-column width="330" fixed="right" label="操作">
        <template #default="scoped">
          <el-button type="primary" v-rule="'stencils-configListEdit'" @click="handleForm(scoped.row,rankingFormRef)">编辑榜单</el-button>
          <el-button type="primary" v-rule="'stencils-configListReward'" @click="handleForm(scoped.row,rewardListRef)">配置奖励</el-button>
          <el-button type="danger" v-rule="'stencils-configListDel'" @click="handleDelete(scoped.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <rankingForm ref="rankingFormRef" @update-list="getList" />
    <rewardList ref="rewardListRef" @update-list="getList" />
  </div>
  
</template>